<template>
    <div>
        <el-dialog title="数据过滤" :visible.sync="dialogVisible" width="500px" v-dialogDrag>
            <el-form ref="filterForm" :model="localFilterData" label-width="120px">
                <el-form-item label="测值">
                    <el-checkbox v-model="localFilterData.h">初始值</el-checkbox>
                    <el-checkbox v-model="localFilterData.m">中间值</el-checkbox>
                    <el-checkbox v-model="localFilterData.n">成果值</el-checkbox>
                </el-form-item>
                <el-form-item label="用户审核状态">
                    <el-checkbox v-model="localFilterData.e" @change="handleUserAuditChange" 
                                 :disabled="localFilterData.k || localFilterData.l">未审核</el-checkbox>
                    <el-checkbox v-model="localFilterData.f" @change="handleUserAuditChange" 
                                 :disabled="localFilterData.k || localFilterData.l">审核通过</el-checkbox>
                    <el-checkbox v-model="localFilterData.g" @change="handleUserAuditChange" 
                                 :disabled="localFilterData.k || localFilterData.l">审核未通过</el-checkbox>
                </el-form-item>
                <el-form-item label="系统审核状态">
                    <el-checkbox v-model="localFilterData.k" 
                                 :disabled="hasUserAuditSelected">审核通过</el-checkbox>
                    <el-checkbox v-model="localFilterData.l" 
                                 :disabled="hasUserAuditSelected">审核未通过</el-checkbox>
                </el-form-item>
                <el-form-item label="测值状态">
                    <el-checkbox v-model="localFilterData.a" :disabled="valueStatusDisabled.normal">正常</el-checkbox>
                    <el-checkbox v-model="localFilterData.b" :disabled="valueStatusDisabled.abnormal">异常</el-checkbox>
                    <el-checkbox v-model="localFilterData.c" :disabled="valueStatusDisabled.error">错误</el-checkbox>
                    <el-checkbox v-model="localFilterData.d" :disabled="valueStatusDisabled.undetermined">未判定</el-checkbox>
                </el-form-item>
            </el-form>
            <div class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small" icon="el-icon-circle-close" class="cancel-btn">取消</el-button>
                <el-button type="primary" @click="filteringSubmit" size="small" icon="el-icon-circle-check" class="confirm-btn">保存配置</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
// 测值类型常量
const VALUE_TYPES = {
    NONE: -1,
    INITIAL: 0,      // h
    MIDDLE: 1,       // m
    FINAL: 2,        // n
    INITIAL_MIDDLE: 3,      // h + m
    INITIAL_FINAL: 4,        // h + n
    MIDDLE_FINAL: 5,         // m + n
    ALL: 6                   // h + m + n
};

export default {
    props: {
        visible: {
            type: Boolean,
            default: false
        },
        filterData: {
            type: Object,
            default: () => ({})
        }
    },
    watch: {
        visible(newVal) {
            this.dialogVisible = newVal;
            if (newVal) {
                this.localFilterData = { ...this.filterData };
            }
        },
        dialogVisible(newVal) {
            this.$emit('update:visible', newVal);
        }
    },
    data() {
        return {
            dialogVisible: this.visible,
            localFilterData: { ...this.filterData }
        };
    },
    computed: {
        // 是否有用户审核状态被选中
        hasUserAuditSelected() {
            return this.localFilterData.e || this.localFilterData.f || this.localFilterData.g;
        },
        // 测值状态的禁用条件
        valueStatusDisabled() {
            const { e, f, g, k, l } = this.localFilterData;
            const hasSystemAudit = k || l;
            const hasUserAuditWithoutE = (f || g) && !e;
            
            return {
                normal: hasSystemAudit || this.hasSpecificUserAuditPattern(e, f, g),
                abnormal: hasSystemAudit || hasUserAuditWithoutE,
                error: hasSystemAudit || hasUserAuditWithoutE,
                undetermined: hasSystemAudit || hasUserAuditWithoutE
            };
        }
    },
    methods: {
        // 计算测值类型
        calculateValueType() {
            const { h, m, n } = this.localFilterData;
            // 使用二进制位运算简化逻辑
            let value = 0;
            if (h) value |= 1; // 第一位
            if (m) value |= 2; // 第二位  
            if (n) value |= 4; // 第三位
            
            // 映射到对应的值类型
            const typeMap = {
                0: VALUE_TYPES.NONE,
                1: VALUE_TYPES.INITIAL,
                2: VALUE_TYPES.MIDDLE,
                3: VALUE_TYPES.INITIAL_MIDDLE,
                4: VALUE_TYPES.FINAL,
                5: VALUE_TYPES.INITIAL_FINAL,
                6: VALUE_TYPES.MIDDLE_FINAL,
                7: VALUE_TYPES.ALL
            };
            
            return typeMap[value] !== undefined ? typeMap[value] : VALUE_TYPES.NONE;
        },
        
        // 处理用户审核状态变化
        handleUserAuditChange() {
            const { e, f, g } = this.localFilterData;
            
            // 重置测值状态
            this.localFilterData.a = false;
            this.localFilterData.b = false;
            this.localFilterData.c = false;
            this.localFilterData.d = false;
            
            // 根据用户审核状态设置测值状态
            if (e) {
                // 未审核选中时，异常、错误、未判定可用
                this.localFilterData.b = true;
                this.localFilterData.c = true;
                this.localFilterData.d = true;
                
                if (f) {
                    // 审核通过也选中时，正常也可用
                    this.localFilterData.a = true;
                }
            } else if (f || g) {
                // 只有审核通过或未通过选中时，所有测值状态不可用
                // 保持全部为false
            }
        },
        
        // 检查特定的用户审核模式（用于正常状态的禁用条件）
        hasSpecificUserAuditPattern(e, f, g) {
            // 这些模式下正常状态应该被禁用
            const patterns = [
                [true, false, false],   // e only
                [false, true, false],   // f only  
                [false, false, true],   // g only
                [true, false, true]     // e + g
            ];
            
            return patterns.some(pattern => 
                e === pattern[0] && f === pattern[1] && g === pattern[2]
            );
        },
        
        // 提交过滤配置
        filteringSubmit() {
            const result = {
                ...this.localFilterData,
                valueType: this.calculateValueType()
            };
            
            this.dialogVisible = false;
            this.$emit('filteringSubmit', result);
        }
    }
};
</script>